package com.cqyc.shop.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cqyc.shop.comm.JdbcUtils;
import com.cqyc.shop.comm.Pager;
import com.cqyc.shop.comm.commException;
import com.cqyc.shop.domain.Cart;

public class CartDao {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	public Cart rowmap(ResultSet rs) throws SQLException {
		Cart cart = new Cart();
		cart.setId(rs.getInt("id"));
		cart.setUsercode(rs.getString("usercode"));
		cart.setShopname(rs.getString("shopname"));
		cart.setCartimg(rs.getString("cartimg"));
		cart.setCartinfo(rs.getString("cartinfo"));
		cart.setCartdate(rs.getDate("cartdate"));
		cart.setCartauthor(rs.getString("cartauthor"));
		cart.setPrice(rs.getBigDecimal("price"));
		cart.setHowmany(rs.getInt("howmany"));
		cart.setBuytotal(rs.getInt("buytotal"));
		return cart;
	}
	/**
	 * 保存商品信息及其用户信息数据到数据库中
	 * @param cart
	 */
	public void save(Cart cart) {
		Connection conn=null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnerction();
			String sql="insert into cart(usercode,shopname,cartimg,cartinfo,cartdate,cartauthor,price,howmany,buytotal) values(?,?,?,?,?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, cart.getUsercode());
			ps.setString(2, cart.getShopname());
			ps.setString(3, cart.getCartimg());
			ps.setString(4, cart.getCartinfo());
			ps.setDate(5,new java.sql.Date(cart.getCartdate().getTime()) );
			ps.setString(6, cart.getCartauthor());
			ps.setBigDecimal(7,cart.getPrice());
			ps.setInt(8, cart.getHowmany());
			ps.setInt(9, cart.getBuytotal());
			int ret = ps.executeUpdate();
			if(ret<0) {
				throw new commException("购物车插入数据失败");
			}
			logger.debug("购物车插入数据成功");
 		} catch (Exception e) {
 			logger.error("{}",e.getMessage());
 			throw new commException("您的商品已经添加到数据库中了哟");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
		
	}
	
	/**
	 * 查询用户购物车表的总数据
	 */
	
	public Integer findCount(Cart cart) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		try {
			conn = JdbcUtils.getConnerction();
			String sql="select count(1) from cart where  usercode=?	 ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, cart.getUsercode());
			rs = ps.executeQuery();
			while (rs.next()) {
				return rs.getInt(1);
			}
		} catch (Exception e) {
			logger.debug("查询购物车失败");
			throw new commException("查询该用户购物车的数量错误");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
		return 0;
	}
	/**
	 * 从购物车表中查询对应的用户的商品信息
	 * @param cart
	 * @param pager 
	 * @return
	 */
	public List<Cart> findPager(Cart cart, Pager pager) {
		Connection conn=null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnerction();
			String sql="select * from cart where usercode=?	  limit ?,? ";
			ps = conn.prepareStatement(sql);
			ps.setString(1, cart.getUsercode());
			ps.setInt(2,  (pager.getCurrentPage()-1)*pager.getPageSize());
			ps.setInt(3,  pager.getPageSize());
			rs = ps.executeQuery();
			List<Cart> carts = new ArrayList<>();
			while (rs.next()) {
				carts.add(rowmap(rs));
			}
			
			Integer totalSize = findCount(cart);
			pager.setTotalSize(totalSize);
			logger.debug("{}",pager.getTotalSize());
			return carts;
		} catch (Exception e) {
			logger.debug("查询数据库失败");
			throw new commException("购物还是空空如也哟，主人快点去购物吧！！");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
	}
	
	/**
	 * 先从数据库查询是否该用户拥有该产品
	 * @param cart
	 * @return
	 */
	public List<Cart> findPager(Cart cart) {
		Connection conn=null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnerction();
			String sql="select * from cart where usercode=?	 and shopname=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, cart.getUsercode());
			ps.setString(2, cart.getShopname());
			rs = ps.executeQuery();
			List<Cart> carts = new ArrayList<>();
			while (rs.next()) {
				carts.add(rowmap(rs));
			}
			return carts;
		} catch (Exception e) {
			throw new commException("购物还是空空如也哟，主人快点去购物吧！！");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
	}
	/**
	 * 根据id来删除数据
	 * @param idd
	 */
	public void delete(Integer idd) {
		Connection conn=null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnerction();
			String sql = "delete from  cart where id=?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, idd);
			int ret = ps.executeUpdate();
			if(ret<0) {
				throw new commException("删除失败");
			}
			logger.debug("删除成功");
		} catch (Exception e) {
			throw new commException("主人，人家删除出问题了嘛");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
	}
	
	/**
	 * 根据id来查找购物车是否有这个数据
	 * @param idd
	 * @return
	 */
	public Cart findId(Integer idd) {
		Connection conn=null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnerction();
			String sql="select * from cart  where id=?";
			ps =conn.prepareStatement(sql);
			ps.setInt(1, idd);
			rs = ps.executeQuery();
			while (rs.next()) {
				return rowmap(rs);
			}
		} catch (Exception e) {
			throw new commException("数据库查找不到相应数据");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
		return null;
	}
	/**
	 * 点击增加按钮修改价格
	 * @param cart
	 */
	public void update(Cart cart) {
		Connection conn=null;
		PreparedStatement ps =null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnerction();
			String sql="update cart  set  price =?  where id=?";
			ps = conn.prepareStatement(sql);
			ps.setBigDecimal(1, cart.getPrice());
			ps.setInt(2, cart.getId());
			int ret = ps.executeUpdate();
			if(ret < 0) {
				throw new commException("修改失败");
			}
		} catch (Exception e) {
			logger.debug("修改失败");
		}finally {
			JdbcUtils.close(conn, ps, rs);
		}
	}
		
}
